package com.zy.dp.iterator.example7;

import java.util.ArrayList;
import java.util.Collection;

public class ArrayIteratorImpl implements AggregationIterator
{
	private PayModel[] pms = null;
	private int index = 0;

	public ArrayIteratorImpl(PayModel[] pms)
	{

		this.pms = pms;
	}

	public ArrayIteratorImpl(SalaryManager aggregate)
	{
		this.pms = aggregate.getPays();
	}

	@Override
	public boolean hasNext()
	{
		if (pms != null && index <= (pms.length - 1))
		{
			return true;
		}

		return false;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Collection next(int num)
	{
		Collection collection = new ArrayList();
		int count = 0;

		while (hasNext() && count < num)
		{
			collection.add(pms[index]);
			index++;
			count++;
		}

		return collection;
	}

	@Override
	public boolean hasPrevious()
	{
		if (pms != null && index > 0)
		{
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Collection previous(int num)
	{
		Collection collection = new ArrayList();
		int count = 0;
		index = index - num;

		while (hasPrevious() && count < num)
		{
			collection.add(pms[index]);
			index++;
			count++;
		}

		return collection;

	}

}
